*--- Prepare Land Registry data
use "${datadir}\landreg_selected_no_Wales_prevnew", clear // 140s

keep if year > 2012

keep if new
ren price purchaseprice
duplicates drop postcode purchaseprice dated, force	

save "${datadir}\landreg_sel_new_no_Wales_post2012", replace



*--- Open EL data and merge
use "${datadir}\HCA_full.dta", clear
ren dated dated_hca
duplicates drop pcd purchaseprice dated, force	

joinby pcd purchaseprice using ///
	"${datadir}\landreg_sel_new_no_Wales_post2012" 
	
// unique schemeid
gen diff_date = dated - dated_hca
bysort schemeid: egen mindiff = min(abs(diff_date))
keep if abs(diff_date) == mindiff

// unique unit_id dated
bysort unit_id dated: egen mindiff2 = min(abs(diff_date))
keep if abs(diff_date) == mindiff2

sum diff_date, det
keep if inrange(diff_date, r(p1), r(p99))

duplicates drop schemeid, force		
duplicates drop unit_id dated, force


keep unit_id dated schemeid
merge 1:m unit_id dated using "${datadir}\landreg_selected_no_Wales_prevnew"

save "${datadir}\landreg_matched_big", replace



*--- Post 2012 files
bysort unit_id: egen minyear = min(year)
preserve
	keep if minyear > 2012
	save "${datadir}\landreg_matched_big_post_2012", replace
restore 

bysort unit_id: gen maxn = _N
keep if maxn > 1
save "${datadir}\landreg_matched", replace


keep if minyear > 2012
save "${datadir}\landreg_matched_post_2012", replace